package com.anzia.study_10.slice;

import com.anzia.study_10.ResourceTable;
import com.anzia.study_10.data.BookStore;
import com.anzia.study_10.data.MyOrmObjectObserver;
import com.anzia.study_10.data.model.User;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmObject;
import ohos.data.orm.OrmPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;

import java.util.Arrays;
import java.util.List;

/**
 * 官方文档位置
 * https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-orm-guidelines-0000000000030063
 */
public class MainAbilitySlice extends AbilitySlice {
	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		// 在前面的配置和数据库类，数据库表类都弄好的前提下，创建数据库和表
		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		OrmContext bookStore = databaseHelper.getOrmContext("BookStore", "BookStore.db", BookStore.class);

		// 注册观察者
		bookStore.registerEntityObserver("user", new MyOrmObjectObserver());

		// 新增数据
		User user = new User();
		user.setFirstName("A");
		user.setLastName("anzia");
		user.setAge(18);
		user.setBalance(1000);
		boolean brs1 = bookStore.insert(user); // 这里的插入是在内存，没有真正的把数据落地到数据库
		boolean brs2 = bookStore.flush(); // 这个方法才是真正的落地到仓库
		System.out.println("=====rs：" + brs1 + "，" + brs2);

		User user1 = new User();
		user.setFirstName("An");
		user.setLastName("Hui");
		user.setAge(28);
		user.setBalance(2000);
		boolean brs11 = bookStore.insert(user); // 这里的插入是在内存，没有真正的把数据落地到数据库
		boolean brs21 = bookStore.flush(); // 这个方法才是真正的落地到仓库
		System.out.println("=====rs：" + brs11 + "，" + brs21);

		// 备份数据库
		bookStore.backup("backupdb.db");

		OrmPredicates id = bookStore.where(User.class).equalTo("id", 1);
		List<OrmObject> query = bookStore.query(id);
		bookStore.delete(query.get(0));
		boolean flush = bookStore.flush();
		System.out.println("======" + flush);

		// 还原数据库
		bookStore.restore("backupdb.db");

		bookStore.close();

		// 删除数据库
		databaseHelper.deleteRdbStore("RdbStore.db");

		// bookStore.beginTransaction();
		// try {
		// 	User user1 = new User();
		// 	user1.setFirstName("wu");
		// 	user1.setLastName("han");
		// 	user1.setAge(28);
		// 	user1.setBalance(2000);
		// 	boolean brs11 = bookStore.insert(user1); // 这里的插入是在内存，没有真正的把数据落地到数据库
		// 	boolean brs21 = bookStore.flush(); // 这个方法才是真正的落地到仓库
		// 	double c = 1/0;
		// 	System.out.println("=====rs：" + brs11 + "，" + brs21);
		// 	// 获取数据，查询数据
		// 	OrmPredicates ormPredicates = bookStore.where(User.class).equalTo("id", 1);
		// 	List<User> drs1 = bookStore.query(ormPredicates);
		// 	User user11 = drs1.get(0);
		// 	bookStore.delete(user11);
		// 	System.out.println("=======" + drs1);
		// 	bookStore.flush();
		// 	bookStore.commit();
		// } catch (Exception e) {
		// 	System.out.println("======出异常了，下面进行数据回滚");
		// 	bookStore.rollback();
		// 	e.printStackTrace();
		// }

		// ResultSet drs2 = bookStore.query(ormPredicates, new String[]{"id", "firstName"});
		// System.out.println("=====" + Arrays.toString(drs2.getAllColumnNames()));

		// 数据修改和删除类似的做法，都有两种操作方式
		// 修改
		// User user1 = drs1.get(0);
		// user1.setFirstName("a");
		// boolean urs1 = bookStore.update(user1);
		// boolean frs1 = bookStore.flush();

		// ValuesBucket valuesBucket = new ValuesBucket();
		// valuesBucket.putString("firstName", "long");
		// OrmPredicates idOrmPredicates = bookStore.where(User.class).equalTo("id", 1);
		// bookStore.update(idOrmPredicates, valuesBucket);

		// 删除
		// User user1 = drs1.get(0);
		// bookStore.delete(user1);
		// bookStore.flush();

		// OrmPredicates deleteIdPre = bookStore.where(User.class).equalTo("id", 1);
		// bookStore.delete(deleteIdPre);
		//
		// OrmPredicates ormPredicates1 = bookStore.where(User.class).equalTo("id", 1);
		//
		// List<User> drs3 = bookStore.query(ormPredicates1);
		// System.out.println("=======:" + drs3);


	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}
}
